<?
require 'global.php';

function encodeUTF8(&$val){
	$val = utf8_encode($val);
}

$pid=0; if (isset($_GET["pid"])) $pid = clean($_GET["pid"]);
$color_oid=0; if (isset($_GET["color_oid"])) $color_oid = clean($_GET["color_oid"]);
$size_oid=0; if (isset($_GET["size_oid"])) $size_oid = clean($_GET["size_oid"]);
$other1_oid=0; if (isset($_GET["other1_oid"])) $other1_oid = clean($_GET["other1_oid"]);
$other2_oid=0; if (isset($_GET["other2_oid"])) $other2_oid = clean($_GET["other2_oid"]);

$do=''; if (isset($_GET["do"])) $do = clean($_GET["do"]);
$url=''; if (isset($_GET["url"])) $url = clean($_GET["url"]);

//echo $pid.'/'.$varid.'/'.$qty.'/'.$do;
//exit;

$data=array();
if($do=='get_related_price'){
	$arr_url=explode(',', $url);
	$where=array();
	$where[]=array('name'=>'product_id','val'=>$arr_url[0]);
	if($arr_url[1]>0){
		$where[]=array('name'=>'color','val'=>$arr_url[1]);
	}
	elseif($arr_url[2]>0){
		$where[]=array('name'=>'size','val'=>$arr_url[2]);
	}
	elseif($arr_url[3]>0){
		$where[]=array('name'=>'other1','val'=>$arr_url[3]);
	}
	elseif($arr_url[4]>0){
		$where[]=array('name'=>'other2','val'=>$arr_url[4]);
	}
	$tem=$product_variation->get_rows_advanced(1,$where);
	//$data['price']=round($tem[0]['price']*$currency_rate,2);
	$data['price']=price($tem[0],1);
	$data['varid']=$tem[0]['id'];
}

if($do=='get_related_options'){
	$where=array();
	$where[]=array('name'=>'product_id','val'=>$pid);
	if($color_oid>0){
		$where[]=array('name'=>'color','val'=>$color_oid);
	}
	elseif($size_oid>0){
		$where[]=array('name'=>'size','val'=>$size_oid);
	}
	elseif($other1_oid>0){
		$where[]=array('name'=>'other1','val'=>$other1_oid);
	}
	elseif($other2_oid>0){
		$where[]=array('name'=>'other2','val'=>$other2_oid);
	}
	
	$res=$product_variation->get_rows_advanced(100,$where);
	
	$color_arr=array();
	$size_arr=array();
	$other1_arr=array();
	$other2_arr=array();
	
	foreach($res as $row) {
		array_push($color_arr,$row['color']);
		array_push($size_arr,$row['size']);
		array_push($other1_arr,$row['other1']);
		array_push($other2_arr,$row['other2']);
	}
	
	$color_arr=array_values(array_unique($color_arr));
	$size_arr=array_values(array_unique($size_arr));
	$other1_arr=array_values(array_unique($other1_arr));
	$other2_arr=array_values(array_unique($other2_arr));
	

	$data['size']=$size_arr;
	$data['color']=$color_arr;
	$data['other1']=$other1_arr;
	$data['other2']=$other2_arr;
	//print_r($color_arr);
	//print_r($size_arr);
	//print_r($other1_arr);
	//print_r($other2_arr);
}

array_walk_recursive($data, 'encodeUTF8');


echo json_encode($data);


?>